package com.ljkj.cordial.chat.dao.notify;

import android.text.TextUtils;

import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.ljkj.cordial.chat.base.ChatApp;
import com.ljkj.cordial.chat.dao.DbController;
import com.ljkj.cordial.chat.dao.FriendTableDao;
import com.ljkj.cordial.chat.util.DeleteHistoryUtil;
import com.ljkj.cordial.chat.util.ObserverThread;
import com.nic.view.util.contactList.PinYinSortBean;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Index;
import org.greenrobot.greendao.annotation.Property;
import org.greenrobot.greendao.annotation.Transient;

import java.io.Serializable;
import java.util.List;

/**
 * 好友消息
 */
@Entity(nameInDb = "FriendTable")
public class FriendTable extends PinYinSortBean implements Serializable {
    private static final long serialVersionUID = -1320389327786493684L;
    @Id(autoincrement = true)
    private Long id;
//    // 当前用户类型
//    @Property(nameInDb = "CURRENT_USER_TYPE")
//    private String currentUserType;

    // 好友ID
    @Property(nameInDb = "FRIEND_ID")
    private String friendId;

    // 好友用户ID
    @Index(unique = true)
    @Property(nameInDb = "FRIEND_USER_ID")
    private String friendUserId;

    // 好友昵称
    @Property(nameInDb = "FRIEND_NICKNAME")
    private String friendNickname;

    // 好友头像
    @Property(nameInDb = "FRIEND_AVATAR")
    private String friendAvatar;
    // 好友头像本地id
    @Property(nameInDb = "FRIEND_AVATAR_ID")
    private String friendAvatarId;
    // 好友头像本地id
    @Property(nameInDb = "FRIEND_AVATAR_PATH")
    private String friendAvatarPath;
    // 好友备注
    @Property(nameInDb = "FRIEND_ALIAS")
    private String friendAlias;

    // 好友用户类型
    @Property(nameInDb = "USER_TYPE")
    private String userType;

    // 在线状态
    @Property(nameInDb = "ONLINE_STATUS")
    private String onlineStatus;

    // 是否接收离线消息
    @Property(nameInDb = "OFFLINE_MESSAGE")
    private boolean offlineMessage = true;

    // 阅后即焚
    @Property(nameInDb = "SNAP_CHAT_STATUS")
    private boolean snapchatStatus;

    // 阅后即焚时长
    @Property(nameInDb = "SNAP_CHAT_TIME_LENGTH")
    private int snapchatTimeLength;

    // 阅后即焚时间单位
    @Property(nameInDb = "SNAP_CHAT_TIME_UNIT")
    private int snapchatTimeUnit;
    @Property(nameInDb = "LOGIN_USER_ID")
    private String loginUserId;
    /**
     * 游客好友创建时间
     */
    @Property(nameInDb = "TOURIST_CREATE_TIME")
    private long touristCreateTime;
    @Transient
    private int footState;

    @Generated(hash = 477433129)
    public FriendTable(Long id, String friendId, String friendUserId, String friendNickname, String friendAvatar, String friendAvatarId, String friendAvatarPath, String friendAlias, String userType, String onlineStatus, boolean offlineMessage,
                       boolean snapchatStatus, int snapchatTimeLength, int snapchatTimeUnit, String loginUserId, long touristCreateTime) {
        this.id = id;
        this.friendId = friendId;
        this.friendUserId = friendUserId;
        this.friendNickname = friendNickname;
        this.friendAvatar = friendAvatar;
        this.friendAvatarId = friendAvatarId;
        this.friendAvatarPath = friendAvatarPath;
        this.friendAlias = friendAlias;
        this.userType = userType;
        this.onlineStatus = onlineStatus;
        this.offlineMessage = offlineMessage;
        this.snapchatStatus = snapchatStatus;
        this.snapchatTimeLength = snapchatTimeLength;
        this.snapchatTimeUnit = snapchatTimeUnit;
        this.loginUserId = loginUserId;
        this.touristCreateTime = touristCreateTime;
    }

    @Generated(hash = 1946338299)
    public FriendTable() {
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }


    public String getFriendId() {
        return this.friendId;
    }

    public void setFriendId(String friendId) {
        this.friendId = friendId;
    }

    public String getFriendUserId() {
        return this.friendUserId;
    }

    public void setFriendUserId(String friendUserId) {
        this.friendUserId = friendUserId;
    }

    public String getFriendNickname() {
        return this.friendNickname;
    }

    public void setFriendNickname(String friendNickname) {
        this.friendNickname = friendNickname;
    }

    public String getFriendAvatar() {
        return this.friendAvatar;
    }

    public void setFriendAvatar(String friendAvatar) {
        this.friendAvatar = friendAvatar;
    }

    public String getFriendAlias() {
        return this.friendAlias;
    }

    public void setFriendAlias(String friendAlias) {
        this.friendAlias = friendAlias;
    }

    public String getUserType() {
        return this.userType;
    }

    public void setUserType(String userType) {
        this.userType = userType;
    }

    public String getOnlineStatus() {
        return this.onlineStatus;
    }

    public void setOnlineStatus(String onlineStatus) {
        this.onlineStatus = onlineStatus;
    }

    public boolean getOfflineMessage() {
        return this.offlineMessage;
    }

    public void setOfflineMessage(boolean offlineMessage) {
        this.offlineMessage = offlineMessage;
    }

    public boolean getSnapchatStatus() {
        return this.snapchatStatus;
    }

    public void setSnapchatStatus(boolean snapchatStatus) {
        this.snapchatStatus = snapchatStatus;
    }

    public int getSnapchatTimeLength() {
        return this.snapchatTimeLength;
    }

    public void setSnapchatTimeLength(int snapchatTimeLength) {
        this.snapchatTimeLength = snapchatTimeLength;
    }

    public int getSnapchatTimeUnit() {
        return this.snapchatTimeUnit;
    }

    public void setSnapchatTimeUnit(int snapchatTimeUnit) {
        this.snapchatTimeUnit = snapchatTimeUnit;
    }

    public static void insert(FriendTable table) {
        if (table.getId() != null) {
            DbController.getInstance().getWrite().update(table);
        } else {
            table.setLoginUserId(ChatApp.getInstance().getUserId());
            DbController.getInstance().getWrite().insertOrReplace(table);
        }

    } public static void update(FriendTable table) {
        if (table.getId() != null) {
            DbController.getInstance().getWrite().update(table);
        }

    }

    /**
     * 清理超过24小时的游客好友记录
     */
    public static void clearOverTimeTourist() {
        ObserverThread.create().onNextThread0(new ObserverThread.OnActionListener<Boolean>() {
            @Override
            public void onAction(Boolean obj) {

            }

            @Override
            public Boolean onThreadAction() {
                List<FriendTable> list = DbController.getInstance().getRead().getFriendTableDao()
                        .queryBuilder().where(FriendTableDao.Properties.TouristCreateTime.isNotNull(), FriendTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId())
                                , FriendTableDao.Properties.UserType.eq(IMSContacts.UserType.TOURISTS_USER)).list();
                if (list != null && !list.isEmpty()) {
                    for (FriendTable friendTable : list) {
                        if (friendTable.getTouristCreateTime() != 0L && System.currentTimeMillis() - friendTable.getTouristCreateTime() > 24 * 60 * 60 * 1000) {
                            DeleteHistoryUtil.getInstance().deleteNavChat(friendTable.getFriendUserId());
                        }
                    }
                }
                return null;
            }
        });

    }

    public static Long getTableIdByFriendUserId(String friendUserId) {
        FriendTable fri = DbController.getInstance().getRead().getFriendTableDao()
                .queryBuilder().where(FriendTableDao.Properties.FriendUserId.eq(friendUserId),
                        FriendTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId())).unique();
        if (fri != null) {
            return fri.getId();
        }
        return 0L;
    }

    public static FriendTable getTableByFriendUserId(String friendUserId) {
        if (TextUtils.isEmpty(friendUserId)) {
            return null;
        }
        FriendTable fri = DbController.getInstance().getRead().getFriendTableDao()
                .queryBuilder().where(FriendTableDao.Properties.FriendUserId.eq(friendUserId),
                        FriendTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId())).unique();
        return fri;
    }

    public static FriendTable getRowByFriendUserIdNotNull(String friendUserId) {
        FriendTable u = getTableByFriendUserId(friendUserId);
        return u == null ? new FriendTable() : u;
    }

    public static void deleteFriendById(String friendUserId) {
        if (TextUtils.isEmpty(friendUserId)) {
            return;
        }
        FriendTable fri = getTableByFriendUserId(friendUserId);
        if (fri != null) {
            DbController.getInstance().getWrite().getFriendTableDao().delete(fri);
        }
    }

    public static List<FriendTable> query() {
        return DbController.getInstance().getRead()
                .getFriendTableDao().queryBuilder()
                .where(FriendTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId())).list();
    }

    public static List<FriendTable> search(String keyword) {
        keyword = "%" + keyword + "%";
        return DbController.getInstance().getRead()
                .getFriendTableDao().queryBuilder()
                .where(FriendTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId()))
                .whereOr(FriendTableDao.Properties.FriendNickname.like(keyword), FriendTableDao.Properties.FriendAlias.like(keyword), FriendTableDao.Properties.FriendUserId.like(keyword))
                .list();
    }

    public static boolean isExistFriendById(String id) {
        FriendTable u = DbController.getInstance().getRead()
                .getFriendTableDao().queryBuilder()
                .where(
                        FriendTableDao.Properties.FriendUserId.eq(id), FriendTableDao.Properties.FriendUserId.notEq(ChatApp.getInstance().getUserId()), FriendTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId())).unique();
        if (u != null) {
            return true;
        }
        return false;
    }

    public static List<FriendTable> queryNoCurrentUser() {
        return DbController.getInstance().getRead()
                .getFriendTableDao().queryBuilder()
                .where(
                        FriendTableDao.Properties.FriendUserId.notEq(ChatApp.getInstance().getUserId()), FriendTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId())).list();
    }

    public static List<FriendTable> query(String like) {
        return DbController.getInstance().getRead()
                .getFriendTableDao().queryBuilder()
                .where(FriendTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId()))
                .whereOr(FriendTableDao.Properties.FriendNickname.like("%" + like + "%")
                        , FriendTableDao.Properties.FriendUserId.like("%" + like + "%")).list();
    }

    public String getLoginUserId() {
        return this.loginUserId;
    }

    public void setLoginUserId(String loginUserId) {
        this.loginUserId = loginUserId;
    }

    @Override
    public String sortName() {
        return TextUtils.isEmpty(friendAlias) ? friendNickname : friendAlias;
    }

    public void setFootState(int footState) {
        this.footState = footState;
    }

    public int getFootState() {
        return footState;
    }

    public String getFriendAvatarId() {
        return this.friendAvatarId;
    }

    public void setFriendAvatarId(String friendAvatarId) {
        this.friendAvatarId = friendAvatarId;
    }

    public long getTouristCreateTime() {
        return this.touristCreateTime;
    }

    public void setTouristCreateTime(long touristCreateTime) {
        this.touristCreateTime = touristCreateTime;
    }

    public static List<FriendTable> queryNotInGroup(List<String> groupMemberUserIds) {
        return DbController.getInstance().getRead()
                .getFriendTableDao().queryBuilder()
                .where(FriendTableDao.Properties.LoginUserId.eq(ChatApp.getInstance().getUserId()),
                        FriendTableDao.Properties.FriendUserId.notIn(groupMemberUserIds))
                .list();
    }

    public String getFriendAvatarPath() {
        return this.friendAvatarPath;
    }

    public void setFriendAvatarPath(String friendAvatarPath) {
        this.friendAvatarPath = friendAvatarPath;
    }
}
